//
//  searchTableViewController.m
//  企业信息查询
//
//  Created by 梁中豪 on 16/4/7.
//  Copyright © 2016年 梁中豪. All rights reserved.
//

#import "searchTableViewController.h"
#import "ENPdetail.h"
#import "AppDelegate.h"
#import "News.h"
#import <BmobSDK/Bmob.h>
#import "CustomCell.h"
@interface searchTableViewController ()<
UISearchBarDelegate>{
    NSMutableArray      *_infoMutableArray;
    
}

- (IBAction)cancel:(id)sender; //cancel按钮的响应
@property (weak, nonatomic) IBOutlet UISearchBar *searchbar; //为搜索栏添加输出口
//@property (nonatomic, strong) NSMutableArray *listFilterTeams; //搜索栏过滤


@end

@implementation searchTableViewController

@synthesize data;
@synthesize loc;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    AppDelegate *mydelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    data =mydelegate.CLNAME ;
    loc = mydelegate.place;
    
    //设置搜索栏委托对象为当前视图控制器
    self.searchbar.delegate = self;
   
    _infoMutableArray = [[NSMutableArray alloc] initWithCapacity:1];
   
    //初次进入查询所有数据
    [self filterContentForSearchText:@""];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//点击searchBar旁边的Cancel按钮  退出键盘
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
    [_searchbar resignFirstResponder];
}

#pragma mark Content Filtering
- (void)filterContentForSearchText:(NSString*)searchText;
{
    AppDelegate *mydelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    loc =mydelegate.place;
    if(!searchText||searchText.length<=0)
    {
        [_infoMutableArray removeAllObjects];
        
        BmobQuery *query = [BmobQuery queryWithClassName:data];
        if (![loc isEqual:@"null"]) {
            [query whereKey:@"city" equalTo:loc];
        }
        
        query.limit = 20;
        [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
            
            for (BmobObject *obj in array) {
                News *info    = [[News alloc] init];
                if ([obj objectForKey:@"Ename"]) {
                    info.Ename = [obj objectForKey:@"Ename"];
                }
                if ([obj objectForKey:@"city"]) {
                    info.city = [obj objectForKey:@"city"];
                }
                if ([obj objectForKey:@"call"]) {
                    info.call = [obj objectForKey:@"call"];
                }
                if ([obj objectForKey:@"adress"]) {
                    info.adress = [obj objectForKey:@"adress"];
                }
                if ([obj objectForKey:@"email"]) {
                    info.email = [obj objectForKey:@"email"];
                }
                if ([obj objectForKey:@"lawman"]) {
                    info.lawman = [obj objectForKey:@"lawman"];
                }
                if ([obj objectForKey:@"setmoney"]) {
                    info.setmoney = [obj objectForKey:@"setmoney"];
                }
                if ([obj objectForKey:@"setdate"]) {
                    info.setdate = [obj objectForKey:@"setdate"];
                }
                
                [_infoMutableArray addObject:info];
            }
            
            [self.tableView reloadData];
        }];
        

    }
    else{
        [_infoMutableArray removeAllObjects];
    
    BmobQuery *query = [BmobQuery queryWithClassName:data];
        [query whereKey:@"city" equalTo:loc];
        
        NSArray *array =  @[@{@"adress":@{@"$regex":searchText}},@{@"Ename":@{@"$regex":searchText}}];
    
        [query addTheConstraintByOrOperationWithArray:array];
        
    [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        
        for (BmobObject *obj in array) {
            News *info    = [[News alloc] init];
            if ([obj objectForKey:@"Ename"]) {
                info.Ename = [obj objectForKey:@"Ename"];
            }
            if ([obj objectForKey:@"city"]) {
                info.city = [obj objectForKey:@"city"];
            }
            if ([obj objectForKey:@"call"]) {
                info.call = [obj objectForKey:@"call"];
            }
            if ([obj objectForKey:@"adress"]) {
                info.adress = [obj objectForKey:@"adress"];
            }
            if ([obj objectForKey:@"email"]) {
                info.email = [obj objectForKey:@"email"];
            }
            if ([obj objectForKey:@"lawman"]) {
                info.lawman = [obj objectForKey:@"lawman"];
            }
            if ([obj objectForKey:@"setmoney"]) {
                info.setmoney = [obj objectForKey:@"setmoney"];
            }
            if ([obj objectForKey:@"setdate"]) {
                info.setdate = [obj objectForKey:@"setdate"];
            }

            
            [_infoMutableArray addObject:info];
        }
        
        [self.tableView reloadData];
    }];
    //self.listFilterTeams = [NSMutableArray arrayWithArray:self->_infoMutableArray];
    
    }
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_infoMutableArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"searchresult";
    
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    News *info = (News*)[_infoMutableArray objectAtIndex:indexPath.row];
       cell.mylabel.text= info.Ename;
       cell.citylabel.text = info.city;
    
    return cell;
}

#pragma mark --UISearchBarDelegate 协议方法
//  获得焦点，成为第一响应者
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    [self.searchbar sizeToFit];
    return YES;
}
//点击键盘上的搜索按钮
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    self.searchbar.showsScopeBar = NO;
    [self.searchbar sizeToFit];
    [self.searchbar resignFirstResponder];
}
//当文本内容发生改变时候调用
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [self filterContentForSearchText:self.searchbar.text ];
    [self.tableView reloadData];
}
- (IBAction)cancel:(id)sender {
     [self dismissViewControllerAnimated:YES completion:nil];
}


//选择表视图行时候触发
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"ShowSelectedenp"])
    {
        ENPdetail *detailViewController = segue.destinationViewController;
        NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
        
        News *info = (News*)[_infoMutableArray objectAtIndex:selectedIndex];
        detailViewController.title = info.Ename;
        detailViewController.detailenpname = info.Ename;
        detailViewController.detailcallname = info.call;
        detailViewController.detailmailname = info.email;
        detailViewController.detailadressname = info.adress;
        detailViewController.lawmanname = info.lawman;
        detailViewController.setdatename = info.setdate;
        detailViewController.setmoneyname = info.setmoney;

        
    }
}

@end
